package com.ruoyi.follow_live.controller;

import java.util.*;

import com.ruoyi.zhubo.domain.Anchor;
import com.ruoyi.zhubo.service.IAnchorService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.follow_live.domain.FollowLiveRoom;
import com.ruoyi.follow_live.service.IFollowLiveRoomService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 关注直播间Controller
 * 
 * @author ruoyi
 * @date 2023-04-08
 */
@Controller
@RequestMapping("/follow_live/room")
public class FollowLiveRoomController extends BaseController
{
    private String prefix = "follow_live/room";

    @Autowired
    private IFollowLiveRoomService followLiveRoomService;
    @Autowired
    private IAnchorService anchorService;

    @RequiresPermissions("follow_live:room:view")
    @GetMapping()
    public String room()
    {
        return prefix + "/room";
    }

    /**
     * 查询关注直播间列表
     */
    @RequiresPermissions("follow_live:room:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FollowLiveRoom followLiveRoom)
    {
        startPage();
        Anchor anchor = new Anchor();
        List<Anchor> listA = anchorService.selectAnchorList(anchor);
        List<FollowLiveRoom> followLiveRooms = new ArrayList<>();
        for (Anchor anchor1 : listA) {
            FollowLiveRoom followLiveRoom1 = new FollowLiveRoom();
            followLiveRoom1.setIsLive(anchor1.getSpare());
            if (StringUtils.isNotBlank(anchor1.getSpare1())){
                followLiveRoom1.setRoomId(Integer.valueOf(anchor1.getSpare1()));
            }
            followLiveRoom1.setConcernId(anchor1.getId());
            followLiveRoom1.setConcernName(anchor1.getName());
            followLiveRoom1.setIsTrue("2");
            followLiveRooms.add(followLiveRoom1);
        }
        List<FollowLiveRoom> list = followLiveRoomService.selectFollowLiveRoomList(followLiveRoom);
        Iterator<FollowLiveRoom> iterator = followLiveRooms.iterator();

        while (iterator.hasNext()) {
            FollowLiveRoom liveRoom = iterator.next();  // 获取当前元素
            for (FollowLiveRoom room : list) {
                if (room.getId().equals(liveRoom.getId())) {
                    iterator.remove();   // 删除当前元素

                }
            }
        }
        for (FollowLiveRoom liveRoom : list) {
            followLiveRooms.add(liveRoom);
        }
        Comparator<FollowLiveRoom> comparator = new Comparator<FollowLiveRoom>() {
            @Override
            public int compare(FollowLiveRoom o1, FollowLiveRoom o2) {
                if ("1".equals(o1.getIsTrue())){
                    return -1;
                }
                return 1;
            }
        };

        Collections.sort(followLiveRooms, comparator);
        return getDataTable(followLiveRooms);
    }

    /**
     * 导出关注直播间列表
     */
    @RequiresPermissions("follow_live:room:export")
    @Log(title = "关注直播间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FollowLiveRoom followLiveRoom)
    {
        List<FollowLiveRoom> list = followLiveRoomService.selectFollowLiveRoomList(followLiveRoom);
        ExcelUtil<FollowLiveRoom> util = new ExcelUtil<FollowLiveRoom>(FollowLiveRoom.class);
        return util.exportExcel(list, "关注直播间数据");
    }

    /**
     * 新增关注直播间
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存关注直播间
     */
    @RequiresPermissions("follow_live:room:add")
    @Log(title = "关注直播间", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FollowLiveRoom followLiveRoom)
    {
        return toAjax(followLiveRoomService.insertFollowLiveRoom(followLiveRoom));
    }

    /**
     * 修改关注直播间
     */
    @RequiresPermissions("follow_live:room:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        FollowLiveRoom followLiveRoom = followLiveRoomService.selectFollowLiveRoomById(id);
        mmap.put("followLiveRoom", followLiveRoom);
        return prefix + "/edit";
    }

    /**
     * 修改保存关注直播间
     */
    @RequiresPermissions("follow_live:room:edit")
    @Log(title = "关注直播间", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FollowLiveRoom followLiveRoom)
    {
        return toAjax(followLiveRoomService.updateFollowLiveRoom(followLiveRoom));
    }

    /**
     * 删除关注直播间
     */
    @RequiresPermissions("follow_live:room:remove")
    @Log(title = "关注直播间", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(followLiveRoomService.deleteFollowLiveRoomByIds(ids));
    }
}
